package com.example.bhservice.mailbox.util;

import com.example.bhservice.mailbox.config.MailConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * 邮件工具类
 */
@Slf4j
public class MailUtil {

    /**
     * 发送
     *
     * @param mailConfig    邮箱配置
     * @param fileBO        附件
     * @param receiverMails 接受者邮箱
     */
    public static String send(MailConfig mailConfig, FileBO fileBO, String receiverMails, MultipartFile file) {
        // 构造邮件发送实例
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        applyProperties(mailConfig, mailSender);
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailConfig.getUsername());
            helper.setTo(receiverMails);
            helper.setSubject(fileBO.getSubject());

            // 正文不发送附件
            helper.setText("", false);
            // 添加附件
            // 获取附件名并进行编码
            String fileName = fileBO.getFileName() + fileBO.getExtension();
            String encodedFileName = MimeUtility.encodeText(fileName, "UTF-8", "B");
            System.setProperty("mail.mime.splitlongparameters", "false");

            // 添加附件
            helper.addAttachment(encodedFileName, file);
//            helper.addAttachment( fileBO.getFileName() + fileBO.getExtension(), file);
            mailSender.send(message);
        } catch (Exception e) {
            log.error("邮箱" + receiverMails + "发送失败  ", e);
            return receiverMails;
        }
        return "success";
    }

    private static void applyProperties(MailConfig properties, JavaMailSenderImpl sender) {
        sender.setHost(properties.getHost());
        if (properties.getPort() != null) {
            sender.setPort(properties.getPort());
        }
        sender.setUsername(properties.getUsername());
        sender.setPassword(properties.getPassword());
        sender.setProtocol(properties.getProtocol());
        if (properties.getDefaultEncoding() != null) {
            sender.setDefaultEncoding(properties.getDefaultEncoding().name());
        }
        if (!properties.getProperties().isEmpty()) {
            sender.setJavaMailProperties(asProperties(properties.getProperties()));
        }
    }

    private static Properties asProperties(Map<String, String> source) {
        Properties properties = new Properties();
        properties.putAll(source);
        return properties;
    }

    /**
     * 从字节数组创建DOC文件
     *
     * @param bytes 字节数组
     * @return DOC文件
     * @throws IOException IO异常
     */
    private static File createDocFileFromBytes(byte[] bytes) throws IOException {
        File docFile = File.createTempFile("yuehai", ".doc");

        try (FileOutputStream fos = new FileOutputStream(docFile)) {
            fos.write(bytes);
        }
        return docFile;
    }


}
